import { Subject } from 'rxjs'
import * as io from 'socket.io'

export class SocketIoService {
  emit: Subject<any>
  send: Subject<any>
  io: SocketIO.Server
  constructor() {
    this.io = io()
    this.io.on('connection', (socket: SocketIO.Socket) => {})
  }
}

export class Socket {
  // 发送消息
  emit$: Subject<{ event: string; data: any }>
  // 接受消息
  on$: Subject<any>
  constructor(private socket: SocketIO.Socket) {
    this.emit$.subscribe(res => {
      this.socket.emit(res.event, res.data)
    })
  }

  on(type: string) {
    this.socket.on(type, (data: any) => {
      this.on$.next(data)
    })
  }
}
